import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';
import { MessageService } from '@abp/message/message.service';
import {
    BomChangeContentDto, BomChangeServiceProxy, BomChangePublicMethodServiceProxy,
    CodeFormatDescServiceProxy, CommonServiceProxy, ApproveInput, MoadifManageServiceProxy
} from '@shared/service-proxies/service-proxies';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { BomWatchComponent } from './bom-watch/bom-watch.component';
@Component({
    selector: 'app-bon-change',
    templateUrl: './bon-change.component.html',
    styleUrls: ['./bon-change.component.less']
})
export class BonChangeComponent extends AppComponentBase {
    @ViewChild('BomWatchComponent') BomWatchComponent: BomWatchComponent;
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    //引入子组件
    @ViewChild('createOrEditModal') modal: ModalDirective;
    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @Input() rowdata: any;  //行数据
    @Input() title: string;  //标题
    @Input() flag: boolean;  //查看还是审批
    approvalForm = new ApproveInput();
    approvalList = [];
    saving = false;
    active = false;
    editData = undefined;
    form = new BomChangeContentDto();
    codeFormatId = undefined;
    maintTitle = undefined;
    xjOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    reasonOption = [
        {
            label: '请选择',
            value: undefined
        }, {
            label: '设计变更',
            value: '设计变更'
        }, {
            label: '工艺变更',
            value: '工艺变更'
        }
    ];
    modelOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    lotNoOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    btOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    changeNoOption = [
        {
            label: '请选择',
            value: undefined
        }
    ];
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _bomChangeServiceProxy: BomChangeServiceProxy,
        private _bomChangePublicMethodServiceProxy: BomChangePublicMethodServiceProxy,
        private _codeFormatDescServiceProxy: CodeFormatDescServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
        private _moadifManageServiceProxy: MoadifManageServiceProxy,
    ) {
        super(injector);
    }
    accept() {
        abp.ui.setBusy();
        //审批同意1
        this.approvalForm.bappId = this.rowdata.bAppId;
        this.approvalForm.operation = 1;
        this._bomChangeServiceProxy.approveChangeBomModif(this.approvalForm)
            .pipe(finalize(() => { }))
            .subscribe(() => {
                abp.ui.clearBusy();
                this._messageService.success('操作成功');
                this.modalSave.emit();
                this.close();
            });
    }
    confues() {
        abp.ui.setBusy();
        //审批拒绝0
        this.approvalForm.bappId = this.rowdata.bAppId;
        this.approvalForm.operation = 0;
        this._bomChangeServiceProxy.approveChangeBomModif(this.approvalForm)
            .pipe(finalize(() => { }))
            .subscribe(() => {
                abp.ui.clearBusy();
                this._messageService.success('操作成功');
                this.modalSave.emit();
                this.close();
            });
    }
    ngOnInit() {
        //获取型号下拉
        this.modelOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.modelOption.push(data);
                }
            });
        // 获取编码规则
        this.changeNoOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._codeFormatDescServiceProxy.getCodeFormatDropList()
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.codeName;
                    data.value = item.id;
                    this.changeNoOption.push(data);
                }
            });
    }
    getLotNo(event) {
        //获取发次
        this.lotNoOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.form.moduleId)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.lotNoOption.push(data);
                }
            });
    }
    //打开弹窗
    onShown(): void {
        this.active = true;
    }
    getClass(data) {
        if (data.result == 0) {
            return 'approval-info';
        } else if (data.result == 1) {
            return 'approval-success';
        } else if (data.result == 2) {
            return 'approval-error';
        }
    }
    //查看新增任务
    show(): void {
        if (this.title == '审批') {
            abp.ui.setBusy();
            this._bomChangeServiceProxy.getBomChangeSearchOne(
                this.rowdata.bAppId
            )
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    abp.ui.clearBusy();
                    this.editData = result[0];
                    this.approvalList = result[0].workAuditorOutput;
                    this.initEditInfo();//初始化数据
                });
        } 
        this.active = true;
        this.modal.show();
    }
    initEditInfo() {
        this.form.updateBTid = this.editData.updateBTid;
        this.form.module = this.editData.module;
        this.form.moduleId = this.editData.moduleId;
        this.lotNoOption = [
            {
                label: '请选择',
                value: undefined
            }
        ];
        this._commonServiceProxy.getLotNoList(this.form.moduleId)
            .pipe(finalize(() => { }))
            .subscribe(result => {
                for (const item of result) {
                    let data = { label: undefined, value: undefined };
                    data.label = item.label;
                    data.value = item.value;
                    this.lotNoOption.push(data);
                }
                this.form.sendTime = this.editData.sendTime;
                this.form.sendTimeId = this.editData.sendTimeId;
                //获取芯级
                this.xjOption = [
                    {
                        label: '请选择',
                        value: undefined
                    }
                ];
                this._commonServiceProxy.getBomXj(
                    this.editData.moduleId,
                    this.editData.sendTimeId,
                    undefined,
                    undefined
                ).pipe(finalize(() => { }))
                    .subscribe(result => {
                        for (const item of result) {
                            let data = { label: undefined, value: undefined };
                            data.label = item.label;
                            data.value = item.value;
                            this.xjOption.push(data);
                        }
                        this.form.xjId = this.editData.xjId;
                        this.form.xjName = this.editData.xjName;
                        // 获取布套
                        this.btOption = [
                            {
                                label: '请选择',
                                value: undefined
                            }
                        ];
                        this._commonServiceProxy.getBomBt(
                            this.editData.moduleId,
                            this.editData.sendTimeId,
                            undefined,
                            this.form.xjId
                        ).pipe(finalize(() => { }))
                            .subscribe(result => {
                                for (const item of result) {
                                    let data = { label: undefined, value: undefined };
                                    data.label = item.label;
                                    data.value = item.value;
                                    this.btOption.push(data);
                                };
                                this.form.btId = this.editData.btId;
                            });
                    });
            });
        this.form.id = this.editData.id;
        this.form.beforeVersion = this.editData.laterVersion;
        this.form.reasonsChange = this.editData.reasonsChange;
        this.form.reasonsChangeExplain = this.editData.reasonsChangeExplain;
        this.codeFormatId = this.editData.codeId;
        this.form.changeContent = this.editData.changeContent;
    }
    backToApproval() {
    }
    //关闭事件
    close(): void {
        //清空form表单
        this.approvalForm = new ApproveInput();
        this.form = new BomChangeContentDto();
        this.active = false;
        this.modal.hide();
    }
    toMaintain() {
        this.maintTitle = 'BOM查看';
        setTimeout(() => {
            this.BomWatchComponent.show();
        }, 10);
    }
    saveToapproval() {
    }
    save() {

    }
    onChange(event) {

    }
    getBt(event) {
    }
}
